package com.myworkspace.standard.common;

import com.myworkspace.standard.common.lock.DSLock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) {

        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("aa", "bb");
        list.add(map);
//        map = null;
        // java 的接口传递 是把 值（内存地址） 传入了 在内存地址做的变更会 同步因为是同一片内存地址，当=null 是只是把当前的对象指向了null而不是把内存地址清空
        // 内存地址存的东西还在内存中，等待垃圾回收
        System.out.println(list.get(0) == map);

    }

    public void testLock() {
        DSLock.lock("test_lock");
        DSLock.unLock("test_lock");
    }


    public static void proxyTest() {
        Son son = new Son();
        son.getKey("对象处理前");
        try {
            son.say();
        } catch (Exception e) {
            System.out.println("异常");
        }
        son.getKey("对象处理后");


        ParentInterface proxy = new Proxy(new Son());
        proxy.getKey("代理处理前");
        proxy.say();
        proxy.getKey("代理处理前");
    }

    public interface ParentInterface {
        void say(); // 实现

        void rollback();

        void getKey(String s);
    }

    public static class Son implements ParentInterface {
        public Integer i = 20;

        @Override
        public void getKey(String s) {
            System.out.println(s + "输出" + i);
        }

        public void say() {
            i -= 10;
            int k = 10 / 0;
        }

        public void rollback() {
            i += 10;
        }
    }

    public static class Proxy implements ParentInterface {
        private ParentInterface target;

        @Override
        public void getKey(String s) {
            target.getKey(s);
        }

        public Proxy(ParentInterface target) {
            this.target = target;
        }

        @Override
        public void rollback() {
            target.rollback();
        }

        public void say() {
            System.out.println("开启事务");
            try {
                target.say();
            } catch (Exception e) {
                System.out.println("回滚事务");
                this.rollback();
            } finally {
                System.out.println("结束事务");
            }
        }
    }
}


